<template>
	<div class="container">
		<!-- 路径导航区域 -->
		<el-breadcrumb separator="/">
			<el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
			<el-breadcrumb-item>咨询师管理</el-breadcrumb-item>
		</el-breadcrumb>
		<!-- 搜索区域 -->
		<el-row>
			<el-form :inline="true">
				<el-form-item label="咨询师名称">
					<el-input v-model="queryInfo.name" clearable @clear="queryList" placeholder="请输入咨询师名称"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="queryList">搜索</el-button>
					<el-button type="primary" @click="toAddConsult">新增咨询师</el-button>
					<el-button type="danger" @click="deleteBatch">批量删除</el-button>
				</el-form-item>
			</el-form>
		</el-row>
		<!-- 新增和修改的对话框 -->
		<!-- dialog中绑定close事件，实现对话框右上角叉号的取消功能 -->
		<el-dialog :title="title" :visible.sync="dialogFormVisible" @close="cancle">
			<el-form :model="consult">
				<el-form-item label="咨询师名称" label-width="100px">
					<el-input placeholder="请输入咨询师名称" v-model="consult.name"></el-input>
				</el-form-item>
				<el-form-item label="咨询师性别" label-width="100px">
					<el-input placeholder="请输入咨询师性别" v-model="consult.sex"></el-input>
				</el-form-item>

				<el-form-item label="咨询师等级" label-width="100px">
					<el-input placeholder="请输入咨询师等级" v-model="consult.level_id"></el-input>
				</el-form-item>
				<el-form-item label="咨询师头像" label-width="100px">
					<!-- el-upload:上传组件 -->
					<!-- action: 上传网址 list-type: 上传组件类型 limit: 限制上传图片个数 fileList: 显示的图片集合
							on-success: 上传成功后的回调函数 on-preview: 图片预览事件 on-remove: 移除图片事件 -->
					<el-upload :action="url + '/upload'" list-type="picture-card" :limit="1" :file-list="fileList"
						:on-success="handleSuccess" :on-preview="handlePictureCardPreview" :on-remove="handleRemove">
						<i class="el-icon-plus"></i>
					</el-upload>
					<!-- el-dialog : 预览图片组件 -->
					<el-dialog :visible.sync="dialogVisible">
						<img width="100%" :src="dialogImageUrl" alt="">
					</el-dialog>
				</el-form-item>
				<el-form-item label="咨询师地址" label-width="100px">
					<el-cascader v-model="address" :options="options"></el-cascader>
				</el-form-item>

				<el-form-item label="咨询师简介" label-width="100px">
					<el-input placeholder="请输入咨询师简介" v-model="consult.intro"></el-input>
				</el-form-item>
				<el-form-item label="证书图片" label-width="100px">
					<el-upload :action="url + '/upload'" list-type="picture-card" :limit="1" :file-list="fileList_book"
						:on-success="handleSuccess_book" :on-preview="handlePictureCardPreview"
						:on-remove="handleRemove_book">
						<i class="el-icon-plus"></i>
					</el-upload>
					<!-- el-dialog : 预览图片组件 -->
					<el-dialog :visible.sync="dialogVisible">
						<img width="100%" :src="dialogImageUrl" alt="">
					</el-dialog>
				</el-form-item>

			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="cancle">取 消</el-button>
				<el-button type="primary" @click="addConsult">确 定</el-button>
			</div>
		</el-dialog>

		<!-- 表格区域 -->
		<el-table ref="multipleTable" @selection-change="handleSelectionChange" :data="tableData" border
			style="width: 100%;" :header-cell-style="{'text-align': 'center'}" :cell-style="{'text-align': 'center'}">
			<el-table-column type="selection" width="55"></el-table-column>
			<el-table-column prop="id" label="ID" width="100"></el-table-column>
			<el-table-column prop="name" label="咨询师名称">
			</el-table-column>
			<el-table-column prop="sex" label="性别">
			</el-table-column>

			<el-table-column prop="consultlevel.name" label="咨询等级">
			</el-table-column>
			<el-table-column prop="img" label="头像">
				<template slot-scope='scope'>
					<el-image style="width: 100px; height: 100px"
						:src="url + 'downLoad?filename='+scope.row.img"></el-image>
				</template>
			</el-table-column>
			<el-table-column prop="address" label="地址">
			</el-table-column>

			<el-table-column prop="intro" label="个人简介">
			</el-table-column>
			<el-table-column prop="authentication" label="是否认证">
			</el-table-column>
			<el-table-column prop="book" label="证书图片">
				<template slot-scope='scope'>
					<el-image style="width: 100px; height: 100px"
						:src="url + 'downLoad?filename='+scope.row.book"></el-image>
				</template>
			</el-table-column>

			<el-table-column label="操作" width="100">
				<template slot-scope='scope'>
					<el-link type="primary" @click="toEditConsult(scope.row)">编辑</el-link><br />
					<el-link type="primary" @click="deleteConsult(scope.row.id)">删除</el-link>
				</template>
			</el-table-column>
		</el-table>
		<!-- 分页区域 -->
		<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
			:current-page="queryInfo.pageNum" :page-sizes="[1, 5, 10, 20]" :page-size="queryInfo.pageSize"
			layout="total, sizes, prev, pager, next, jumper" :total="total">
		</el-pagination>
	</div>
</template>
<script>
	import citys from '../../assets/data/citydata.js'
	export default {
		data() {
			return {
				// 地址
				options: citys,
				// 查询信息对象
				queryInfo: {
					name: '',
					pageNum: 1,
					pageSize: 5
				},
				total: 10,
				// 表格数据
				tableData: [],
				fileList: [],
				fileList_book: [],
				// 图片服务器地址
				url: '',
				// 预览图片路径
				dialogImageUrl: '',
				// 预览图片的对话框
				dialogVisible: false,
				// 批量删除
				ids: null,
				// 对话框标题
				title: '',
				// 是否显示对话框
				dialogFormVisible: false,
				// 地址
				address: [],
				// 咨询师对象 consult
				consult: {
					"id": '',
					"name": "",
					"sex": "",

					"level_id": "1",
					"img": "",
					"address": "",

					"intro": "",
					"authentication": 0,
					"book": "",
				},
			}
		},
		methods: {
			// 分页查询信息的方法
			queryList() {
				// 调用后端接口来获取数据
				this.$http.get('consult/find', {
						params: this.queryInfo
					})
					.then(resp => {
						if (resp.data.status == 200) {
							// console.log(resp.data)
							this.tableData = resp.data.data.list;
							this.total = resp.data.data.total;
							// console.log(this.tableData)
						} else {
							this.$message.error('查询失败！')
						}
					})
					.catch((error) => {
						this.$message.error('服务器失联' + error)
					})
			},
			// 地址格式
			setAddress(row) {
				this.consult.address = row[0] + "-" + row[1] + "-" + row[2];
			},
			getAddress(row) {
				let s = row.split("-")
				// console.log(s)
				this.address = [s[0], s[1], s[2]];
			},
			// 进入添加的对话框
			toAddConsult() {
				this.setAddress(this.address)
				this.title = "新增咨询师";
				this.dialogFormVisible = true;
			},
			// 进入修改的预览数据页面
			toEditConsult(row) {
				// console.log(row)
				// 赋值
				this.consult = row;
				this.getAddress(this.consult.address)
				// 设置标题
				this.title = "修改咨询师";
				// 开启对话框
				this.dialogFormVisible = true;
				// 动态的获取图片的名称并帮到到fileList
				this.fileList = [{
					name: row.img,
					url: this.url + 'show?filename=' + row.img
				}];
				this.fileList_book = [{
					name: row.book,
					url: this.url + 'show?filename=' + row.book
				}];
			},
			// 添加咨询师
			addConsult() {
				console.log("添加咨询师")
				if (this.consult.id) { // 有值为true  修改
					this.setAddress(this.address)
					this.$http.put("consult/edit", this.consult)
						.then(resp => {
							if (resp.data.status == 200) {
								// 消息提示
								this.$message.success('修改成功！');
								// 清空数据
								this.consult = { // 咨询师对象
									"id": '',
									"name": "",
									"sex": "",

									"level_id": "1",
									"img": "",
									"address": "",

									"intro": "",
									"authentication": 0,
									"book": "",
								}
								// 关闭对话框
								this.dialogFormVisible = false;
								// 重新查询
								this.queryList()
							} else {
								this.$message.error('接口异常！')
							}
						})
				} else {
					this.$http.post("consult/add", this.consult)
						.then(resp => {
							if (resp.data.status == 200) {
								// 消息提示
								this.$message.success('添加成功！');
								// 清空数据
								this.consult = { // 咨询师对象
									"id": '',
									"name": "",
									"sex": "",

									"level_id": "1",
									"img": "",
									"address": "",

									"intro": "",
									"authentication": 0,
									"book": "",
								}
								// 关闭对话框
								this.dialogFormVisible = false;
								// 重新查询
								this.queryList()
							} else {
								this.$message.error('接口异常！')
							}
						})
				}
				this.fileList = []
				this.fileList_book = []
			},
			// 处理上传成功的函数
			handleSuccess(response) {
				// 把上传后的图片路径，交给咨询师对象的img
				this.consult.img = response.message;
			},
			handleSuccess_book(response) {
				// 把上传后的图片路径，交给咨询师对象的img
				this.consult.book = response.message;
			},
			// 删除图片的事件
			handleRemove(file, fileList) {
				// 移除数据
				this.consult.img = '';
			},
			handleRemove_book(file, fileList) {
				// 移除数据
				this.consult.book = '';
			},
			// 预览图片事件
			handlePictureCardPreview(file) {
				this.dialogImageUrl = file.url;
				this.dialogVisible = true;
			},
			// 取消对话框
			cancle() {
				this.dialogFormVisible = false;
				this.consult = { // 咨询师对象
					"id": '',
					"name": "",
					"sex": "",

					"level_id": "1",
					"img": "",
					"address": "",

					"intro": "",
					"authentication": 0,
					"book": "",
				}
			},
			// 删除咨询师
			deleteConsult(id) {
				this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning'
					})
					.then(() => {
						this.$http.delete('consult/remove/' + id)
							.then(resp => {
								if (resp.data.status == 200) {
									this.$message.success(resp.data.message)
									this.queryList()
								} else {
									this.$message.error(resp.data.message)
								}
							})
					})
					.catch(() => {
						this.$message({
							type: 'info',
							message: '已取消删除'
						})
					})
			},
			// 批量删除
			handleSelectionChange(val) {
				this.ids = val.map(function(item) {
					return item.id;
				});
				// console.log(this.ids);
			},
			// 批量删除咨询师
			deleteBatch() {
				if (this.ids.length == 0) { // 没有选择咨询师列表
					return;
				}
				this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning'
					})
					.then(() => {
						this.$http.delete('consult/removeBatch?ids=' + this.ids)
							.then(resp => {
								if (resp.data.status == 200) {
									this.$message.success(resp.data.message)
									this.queryList()
								} else {
									this.$message.error(resp.data.message)
								}
							})
					})
					.catch(() => {
						this.$message({
							type: 'info',
							message: '已取消删除'
						})
					})
			},
			// 分页
			handleSizeChange(size) {
				//console.log('每页显示数量发生改变');
				this.queryInfo.pageSize = size;
				this.queryInfo.pageNum = 1;
				// 重新查询
				this.queryList()
			},
			handleCurrentChange(num) {
				//console.log('页码发生改变');
				this.queryInfo.pageNum = num;
				// 重新查询
				this.queryList();
			},
		},
		mounted() {
			this.queryList();
			// 动态的获取tool.js中的url，并赋值给当前组件的全局url
			// this.url = url;
			// 原型链引入
			this.url = this.$url;
		},
	}
</script>
<style>
	.el-breadcrumb {
		margin-bottom: 15px;
		padding: 0 0 15px;
		border-bottom: 1px solid #ddd;
	}

	.el-pagination {
		margin-top: 15px;
	}

	.el-link {
		padding: 0 5px;
	}

	/**
	 * 设置富文本编辑器的最小高度，注意style中不能使用scoped 
	**/
	.ql-container {
		min-height: 200px;
	}
</style>